<template>
    <el-dialog
        :title="!dataForm.id ? '新建' : '编辑'"
        :close-on-click-modal="false"
        :visible.sync="visible"
        class="SHZY-dialog SHZY-dialog_center"
        lock-scroll
        width="600px"
    >
        <el-row :gutter="15" class="">
            <el-form
                ref="elForm"
                :model="dataForm"
                size="small"
                label-width="100px"
                label-position="right"
                :rules="rules"
            >
                <el-col :span="24">
                    <el-form-item label="工厂" prop="parentId">
                        <SHZYTreeSelect
                            v-model="dataForm.parentId"
                            placeholder="请选择"
                            clearable
                            :props="parentIdProps"
                            required
                            :options="parentIdOptions"
                        >
                        </SHZYTreeSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="编码" prop="enCode">
                        <el-input
                            v-model="dataForm.enCode"
                            placeholder="请输入"
                            clearable
                            required
                            :style="{ width: '100%' }"
                            maxlength="50"
                            show-word-limit
                        >
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="名称" prop="fullName">
                        <el-input
                            v-model="dataForm.fullName"
                            placeholder="请输入"
                            clearable
                            required
                            :style="{ width: '100%' }"
                            maxlength="50"
                            show-word-limit
                        >
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="排序" prop="sortCode">
                        <el-input-number
                            v-model="dataForm.sortCode"
                            placeholder="数字文本"
                            required
                            :step="1"
                            :min="0"
                            :style="{ width: '100%' }"
                        >
                        </el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="描述" prop="desc">
                        <el-input
                            v-model="dataForm.desc"
                            placeholder="请输入"
                            show-word-limit
                            :style="{ width: '100%' }"
                            type="textarea"
                            :autosize="{ minRows: 4, maxRows: 4 }"
                        >
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="24">
                    <el-form-item label="有效标志" prop="enabledMark">
                        <el-switch
                            v-model="dataForm.enabledMark"
                            :active-value="1"
                            :inactive-value="0"
                        >
                        </el-switch>
                    </el-form-item>
                </el-col>
            </el-form>
        </el-row>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取 消</el-button>
            <el-button
                type="primary"
                @click="dataFormSubmit()"
                :loading="btnLoading"
                >确 定</el-button
            >
        </span>
    </el-dialog>
</template>
<script>
import restAPI from "@/api/q1/basedata/station";
import { getDictionaryDataSelector } from "@/api/systemData/dictionary";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
export default {
    components: {},
    props: [],
    data() {
        const validatePass = (rule, value, callback) => {
            if (!value || value.trim().length == 0) {
                this.$set(this.dataForm, rule.field, "");
                callback(new Error("不能为空"));
            } else {
                callback();
            }
        };
        return {
            btnLoading: false,
            loading: false,
            visible: false,
            dataForm: {
                id: "",
                parentId: undefined,
                enCode: undefined,
                fullName: undefined,
                sortCode: undefined,
                desc: undefined,
                enabledMark: 1
            },
            rules: {
                parentId: [
                    {
                        required: true,
                        message: "请输入工厂",
                        trigger: '"change"'
                    }
                ],
                enCode: [
                    {
                        required: true,
                        message: "请输入编码",
                        validator: validatePass,
                        trigger: '"blur"'
                    }
                ],
                fullName: [
                    {
                        required: true,
                        message: "请输入名称",
                        validator: validatePass,
                        trigger: '"blur"'
                    }
                ],
                sortCode: [
                    {
                        required: true,
                        message: "请输入排序",
                        trigger: ["blur", "change"]
                    }
                ]
            },
            parentIdProps: {
                multiple: false,
                label: "fullName",
                value: "id",
                children: "children"
            },
            parentIdOptions: []
        };
    },
    computed: {},
    watch: {},
    created() {
        this.getparentIdOptions();
    },
    mounted() {},
    methods: {
        getparentIdOptions() {
            restAPI.GetListOption().then(res => {
                let data = res.data;
                this.parentIdOptions = data;
            });
        },
        goBack() {
            this.$emit("refresh");
        },
        init(id) {
            this.dataForm.id = id || 0;
            this.visible = true;
            this.$nextTick(() => {
                this.$refs["elForm"].resetFields();
                if (this.dataForm.id) {
                    restAPI.GetInfo(this.dataForm).then(res => {
                        this.dataForm = res.data;
                        console.log("666", res.data);
                    });
                }
            });
            this.$store.commit("generator/UPDATE_RELATION_DATA", {});
        },
        dataFormSubmit() {
            this.$refs["elForm"].validate(valid => {
                if (valid) {
                    this.btnLoading = true;
                    if (!this.dataForm.id) {
                        restAPI
                            .Create(this.dataForm)
                            .then(res => {
                                this.$message({
                                    message: res.msg,
                                    type: "success",
                                    duration: 1000,
                                    onClose: () => {
                                        this.btnLoading = false;
                                        (this.visible = false),
                                            this.$emit("refresh", true);
                                    }
                                });
                            })
                            .catch(res => {
                                this.btnLoading = false;
                            });
                    } else {
                        restAPI
                            .Update(this.dataForm)
                            .then(res => {
                                this.$message({
                                    message: res.msg,
                                    type: "success",
                                    duration: 1000,
                                    onClose: () => {
                                        this.btnLoading = false;
                                        this.visible = false;
                                        this.$emit("refresh", true);
                                    }
                                });
                            })
                            .catch(res => {
                                this.btnLoading = false;
                            });
                    }
                }
            });
        }
    }
};
</script>
